import { useLocalStorageState } from 'ahooks';

export function useHistory(key: string) {
  const [history, setHistory] = useLocalStorageState<any>(key, {});
  return [
    history,
    (values: any) =>
      setHistory(
        Object.fromEntries(
          Object.entries(values).map(([key, val]) => [
            key,
            [
              ...new Set(
                [val, ...(history?.[key] ?? [])].filter((item) => !!item),
              ),
            ].slice(0, 10),
          ]),
        ),
      ),
  ];
}
